#define  _CRT_SECURE_NO_WARNINGS 1
#include <iostream>

namespace lw
{
	enum Color
	{
		RED,
		BLACK
	};

	template<class T>
	struct RBTreeNode
	{
		RBTreeNode<T>* _parent;
		RBTreeNode<T>* _left;
		RBTreeNode<T>* _right;
		T _data;
		Color _col;
		RBTreeNode(const T& val) : _parent(nullptr), _left(nullptr), _right(nullptr), _data(val), _col(RED){}
		RBTreeNode(T&& val) : _parent(nullptr), _left(nullptr), _right(nullptr), _data(std::move(val)), _col(RED) {}
	};

	template<class T, class Ref, class Ptr>
	struct __rbtree_iterator
	{
		typedef RBTreeNode<T> Node;
		typedef __rbtree_iterator<T, Ref, Ptr> Self;
		Node* _node;
		__rbtree_iterator(Node* node) :_node(node){}
		Self& operator++()
		{
			if (_node->_right)
			{
				_node = _node->_right;
				while (_node->_left)
					_node = _node->_left;
			}
			else
			{
				Node* parent = _node->_parent;
				while (parent && _node == parent->_right)
				{
					_node = parent;
					parent = _node->_parent;
				}
				_node = parent;
			}
			return *this;
		}
		Self& operator--()
		{
			if (_node->_left)
			{
				_node = _node->_left;
				while (_node->_right)
					_node = _node->_right;
			}
			else
			{
				Node* parent = _node->_parent;
				while (parent && _node == parent->_left)
				{
					_node = parent;
					parent = _node->_parent;
				}
				_node = parent;
			}
			return *this;
		}
		Ref operator*()
		{
			return _node->_data;
		}
		Ptr operator->()
		{
			return &(operator*());
		}
		bool operator==(const Self& s) const
		{
			return _node == s._node;
		}
		bool operator!=(const Self& s) const
		{
			return _node != s._node;
		}
	};


	template<class K, class T, class KOfT, class Compare>
	class RBTree
	{
		typedef RBTreeNode<T> Node;
	public:
		typedef __rbtree_iterator<T, T&, T*> iterator;
		typedef __rbtree_iterator<const T, const T&, const T*> const_iterator;
		iterator begin()
		{
			Node* curr = _root;
			while (curr->_left)
				curr = curr->_left;
			return curr;
		}
		iterator end()
		{
			return nullptr;
		}
		const_iterator begin() const
		{
			Node* curr = _root;
			while (curr->_left)
				curr = curr->_left;
			return curr;
		}
		const_iterator end() const
		{
			return nullptr;
		}

		std::pair<iterator, bool> Insert(const T& val)
		{

			if (_root == nullptr)
			{
				_root = new Node(val);
				_root->_col = BLACK;
				return std::make_pair(iterator(_root), true);
			}
			Node* curr = _root, * parent = nullptr;
			while (curr)
			{
				if (Compare()(KOfT()(curr->_data), KOfT()(val)))
				{
					parent = curr;
					curr = curr->_right;
				}
				else if (Compare()(KOfT()(val), KOfT()(curr->_data)))
				{
					parent = curr;
					curr = curr->_left;
				}
				else
					return std::make_pair(iterator(curr), false);
			}
			curr = new Node(val);
			Node* newnode = curr;
			if (parent->_data > val)
				parent->_left = curr;
			else
				parent->_right = curr;
			curr->_parent = parent;
			while (parent && parent->_col == RED)
			{
				Node* grand = parent->_parent;
				if (parent == grand->_left)
				{
					Node* uncle = grand->_right;
					if (uncle && uncle->_col == RED)
					{
						uncle->_col = parent->_col = BLACK;
						grand->_col = RED;
						curr = grand;
						parent = curr->_parent;
					}
					else
					{
						if (curr == parent->_left)
						{
							//			g
							//		p        u
							//  c
							RotatoR(grand);
							parent->_col = BLACK;
							grand->_col = RED;
						}
						else
						{
							//			g
							//		p        u
							//		  c
							RotatoL(parent);
							RotatoR(grand);
							curr->_col = BLACK;
							grand->_col = RED;

						}
						break;
					}
				}
				else
				{
					Node* uncle = grand->_left;
					if (uncle && uncle->_col == RED)
					{
						uncle->_col = parent->_col = BLACK;
						grand->_col = RED;
						curr = grand;
						parent = curr->_parent;
					}
					else
					{
						if (curr == parent->_right)
						{
							//			g
							//		u        p
							//					c
							RotatoL(grand);
							parent->_col = BLACK;
							grand->_col = RED;
						}
						else
						{
							//			g
							//		u        p
							//			  c
							RotatoR(parent);
							RotatoL(grand);
							curr->_col = BLACK;
							grand->_col = RED;
						}
					}
				}
			}
			_root->_col = BLACK;
			return std::make_pair(iterator(newnode), true);
		}
		
	private:
		void RotatoR(Node* parent)
		{
			//			p
			//  subL
			//		subLR

			Node* subL = parent->_left;
			Node* subLR = subL->_right;
			parent->_left = subLR;
			if (subLR)
				subLR->_parent = parent;
			Node* pparent = parent->_parent;
			subL->_right = parent;
			parent->_parent = subL;
			if (pparent == nullptr)
			{
				_root = subL;
				subL->_parent = nullptr;
			}
			else
			{
				if (parent == pparent->_left)
					pparent->_left = subL;
				else
					pparent->_right = subL;
				subL->_parent = pparent;
			}
		}
		void RotatoL(Node* parent)
		{
			//			p
			//				subR
			//			subRL
			Node* subR = parent->_right;
			Node* subRL = subR->_left;
			parent->_right = subRL;
			if (subRL)
				subRL->_parent = parent;
			Node* pparent = parent->_parent;
			subR->_left = parent;
			parent->_parent = subR;
			if (pparent == nullptr)
			{
				_root = subR;
				subR->_parent = nullptr;
			}
			else
			{
				if (parent == pparent->_left)
					pparent->_left = subR;
				else
					pparent->_right = subR;
				subR->_parent = pparent;
			}
		}
	private:
		Node* _root = nullptr;
	};
}

